<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="600"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      label-width="100px"
      label-position="right"
    >
      <sk-tabs type="border-card">
        <el-tab-pane ref="tab" label="设备信息信息">
          <el-row>
            <el-col :span="24">
              <el-form-item label="编码" prop="code">
                <el-input v-model="form.code" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="分组" prop="category_id">
                <skDicSelect
                  v-model="this.form.category_id"
                  :code="'MachineCategory'"
                ></skDicSelect>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="是否启用" prop="status">
                <el-switch
                  v-model="form.status_id"
                  :active-value="0"
                  :inactive-value="-1"
                ></el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="机长" prop="employee_id">
                <skAutoChoose
                  v-model="form.employee.name"
                  :name="'user'"
                  :asKey="'employee_id'"
                  :assign="form"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="排序" prop="sort">
                <el-input v-model="form.sort" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </sk-tabs>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skDicSelect from "@/components/skDicSelect";
import skAutoChoose from "@/components/skAutoChoose";
export default {
  emits: ["success", "closed", "update:modeValue"],
  components: { skDicSelect, skAutoChoose },
  data() {
    return {
      mode: "add",
      diccode: "CostType",
      unit: "All",
      titleMap: {
        add: "新增",
        edit: "编辑",
      },
      form: {
        id: "",
        code: "",
        status_id: 0,
        name: "",
        remark: "",
        details: [],
        employee: {},
        ratios: [],
      },
      rules: {
        name: [{ required: true, message: "请输入名称" }],
        code: [{ required: true, message: "请输入编码" }],
        category_id: [{ required: true, message: "请选择设备分组" }],
      },
      gridEvents: {},
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {},
  methods: {
    //显示
    close() {
      this.visible = false;
      this.$emit("update:modeValue", false);
      this.$emit("closed");
    },
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },

    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.basedata.loom.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, this.$TOOL.removeNull(data));
    },
  },
};
</script>

<style></style>
